package com.shudes.pt.builder;

import java.util.*;

import org.hibernate.*;
import org.hibernate.criterion.*;

import com.shudes.pt.db.*;
import com.shudes.pt.pojo.*;
import com.shudes.util.*;

public class GameBuilder {

	protected SessionContext sessionContext;
	
	public GameBuilder(SessionContext context) {
		this.sessionContext=context;
	}
	
	@SuppressWarnings("unchecked")
	public List<Game> forSession(PTSession ptSession) {
		Session session;
		List<Game> games; 
		
		session = sessionContext.getSession();
		session.beginTransaction(); 
		
		games = 
			session.createCriteria(Game.class)
			.add(Restrictions.gt("datePlayed", ptSession.getStart()))
			.add(Restrictions.lt("datePlayed", ptSession.getEnd()))
			.add(Restrictions.eq("tableName", ptSession.getTableName())).list(); 

		System.out.println(Dumper.INSTANCE.dumpAllSideways(games)); 
		
		return games; 		
	}
	
	@SuppressWarnings("unchecked")
	public List<Game> forOnDay(Date d) 
	{
		Session session;
		List<Game> games; 
		Date start;
		Date end; 
		
		start = Dates.truncateDate(d); 
		end = Dates.endOfDay(d); 
		
		session = sessionContext.getSession();
		session.beginTransaction();
		games = session.createCriteria(Game.class)
			.add(Restrictions.gt("datePlayed", start))
			.add(Restrictions.lt("datePlayed", end)).list();
		
		System.out.println(games.size()); 
		
		return games; 		
	}

}
